package com.zgg.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created HttpClientUtils.java
 *
 * @author liyang
 * @create 2018-07-12
 **/
public class HttpClientUtils {

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HttpClientUtils.class);
    public static final long RELEASE_CONNECTION_WAIT_TIME = 5000;// 监控连接间隔
    private static PoolingHttpClientConnectionManager httpClientConnectionManager = null;
    private static LaxRedirectStrategy redirectStrategy = null;
    private static HttpRequestRetryHandler myRetryHandler = null;
    private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
    private static MyResponseHandler rh = null;

    static {
        initHttpClient();
        // 启动清理连接池链接线程
        Thread idleConnectionMonitorThread = new IdleConnectionMonitorThread(httpClientConnectionManager);
        idleConnectionMonitorThread.setDaemon(true);
        idleConnectionMonitorThread.start();
    }

    public static void initHttpClient() {
        try {
            rh = new MyResponseHandler();
            // 重定向策略初始化
            redirectStrategy = new LaxRedirectStrategy();
            // 请求重试机制，默认重试3次
            myRetryHandler = new HttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                    // 如果已经重试了5次，就放弃
                    if (executionCount >= 5) {
                        return false;
                    }
                    // 如果服务器丢掉了连接，那么就重试
                    if (exception instanceof NoHttpResponseException) {
                        return true;
                    }
                    // 超时
                    if (exception instanceof InterruptedIOException) {
                        return false;
                    }
                    // 目标服务器不可达
                    if (exception instanceof UnknownHostException) {
                        return false;
                    }
                    // 连接被拒绝
                    if (exception instanceof ConnectTimeoutException) {
                        return false;
                    }
                    // ssl握手异常
                    if (exception instanceof SSLException) {
                        // SSL handshake exception
                        return false;
                    }
                    HttpClientContext clientContext = HttpClientContext.adapt(context);
                    HttpRequest request = clientContext.getRequest();
                    // 如果请求是幂等的，就再次尝试
                    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                    if (idempotent) {
                        // Retry if the request is considered idempotent
                        return true;
                    }
                    return false;
                }
            };
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", new PlainConnectionSocketFactory())//
                    .register("https", sslConnectionSocketFactory)//
                    .build();
            // 创建httpclient连接池
            httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
            // 设置连接池最大数量,这个参数表示所有连接最大数。
            httpClientConnectionManager.setMaxTotal(500);
            // 设置单个路由最大连接数量，表示单个域名的最大连接数，
            // 例如:www.baidu.com.www.google.com表示不同的域名,则连接统一域名下的资源的最大连接数就是该参数,总和是上面的参数。
            httpClientConnectionManager.setDefaultMaxPerRoute(100);
        } catch (Exception e) {
            logger.error("初始化httpclient连接池失败.", e);
        }

    }

    public static CloseableHttpClient getHttpClient() {
        // HttpHost ip = ConfigFileUtil.getRandomIp();
        RequestConfig requestConfig = RequestConfig.custom()
                //http请求超时
                .setConnectTimeout(5000)//
                //http连接超时
                .setSocketTimeout(5000)//
                // 忽略cookie,如果不需要登陆最好去掉,否则修改策略保存cookie即可
                .setCookieSpec(CookieSpecs.IGNORE_COOKIES)//
                .setConnectionRequestTimeout(6000)//
                // .setProxy(ip)//设置代理ip,不设置就用本机
                .build();
        // 连接池配置
        CloseableHttpClient httpClient = HttpClients.custom()//
                .setSSLSocketFactory(sslConnectionSocketFactory)//
                .setConnectionManager(httpClientConnectionManager)//
                .setDefaultRequestConfig(requestConfig)//
                .setRedirectStrategy(redirectStrategy)//
                .setRetryHandler(myRetryHandler)//
                .build();

        return httpClient;
    }

    /**
     * get请求 headers表示特殊的请求头
     */
    public static JSONObject getUrlContent(String urlString, Map<String, String>... headers) {
        JSONObject html = new JSONObject();
        HttpGet httpGet = null;
        urlString = urlString.trim();
        if (null == urlString || urlString.isEmpty() || !urlString.startsWith("http")) {
            return html;
        }
        // 转化String url为URI,解决url中包含特殊字符的情况
        try {
            URL url = new URL(urlString);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            httpGet = new HttpGet(uri);
            setCommonHeaders(httpGet);
            // 额外的header
            if (headers != null && headers.length > 0) {
                for (Map.Entry<String, String> header : headers[0].entrySet()) {
                    if (httpGet.containsHeader(header.getKey())) {
                        httpGet.setHeader(header.getKey(), header.getValue());
                    } else {
                        httpGet.addHeader(header.getKey(), header.getValue());
                    }
                }
            }
            HttpClient httpClient = getHttpClient();
            html = httpClient.execute(httpGet, rh);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("请求错误 url=" + urlString);
        } finally {
            httpGet.abort();
        }
        return html;
    }

    /**
     * post请求,params表示参数,headers表示请求头
     */
    public static JSONObject postForEntity(String urlString, Map<String, String> params, Map<String, String>... headers) {
        JSONObject html = new JSONObject();
        HttpPost httpPost = null;
        urlString = urlString.trim();
        try {
            // 参数设置
            if (null == urlString || urlString.isEmpty() || !urlString.startsWith("http")) {
                return html;
            }
            URL url = new URL(urlString);
            URI uri = url.toURI();//new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            httpPost = new HttpPost(uri);
            setCommonHeaders(httpPost);
            // 额外的header
            if (headers != null && headers.length > 0) {
                for (Map.Entry<String, String> header : headers[0].entrySet()) {
                    if (httpPost.containsHeader(header.getKey())) {
                        httpPost.setHeader(header.getKey(), header.getValue());
                    } else {
                        httpPost.addHeader(header.getKey(), header.getValue());
                    }
                }
            }
            if (params != null && params.size() > 0) {
                List<BasicNameValuePair> nvps = new ArrayList<>();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                // 这里设置的是返回结果编码,大多数都是UTF-8,如果乱码,可以查看网页的meta中的content的编码.如果是GBK,这里改为GBK即可.
                // 这里entity只能读取一次,想要读取多次,百度一下.
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            }
            HttpClient httpClient = getHttpClient();
            html = httpClient.execute(httpPost, rh);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("请求错误 url=" + urlString);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            logger.error("请求错误 url=" + urlString);
        } finally {
            httpPost.abort();
        }
        return html;

    }

    private static void setCommonHeaders(AbstractHttpMessage request) {
        request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        // request.addHeader("Connection", "keep-alive");
        request.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
        request.addHeader("Accept-Encoding", "gzip, deflate, br");
        // User-Agent最好随机换
        request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
    }

    /**
     * 响应处理类,处理返回结果
     *
     * @author 王
     */
    public static class MyResponseHandler implements ResponseHandler<JSONObject> {

        @Override
        public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            JSONObject result = new JSONObject();
            try {
                // 返回状态码
                int statusCode = response.getStatusLine().getStatusCode();

                result.put("code", statusCode);
                switch (statusCode) {
                    case 200:
                        result.put("message", EntityUtils.toString(response.getEntity(), "UTF-8"));
                        return result;
                    //case 400:
                    //    logger.error("下载400错误代码，请求出现语法错误.");
                    //    break;
                    //case 403:
                    //    logger.error("下载403错误代码，资源不可用.");
                    //    break;
                    //case 404:
                    //    logger.error("下载404错误代码，无法找到指定资源地址.");
                    //    break;
                    //case 503:
                    //    logger.error("下载503错误代码，服务不可用.");
                    //    break;
                    //case 504:
                    //    logger.error("下载504错误代码，网关超时.");
                    //    break;
                    default:
                        result.put("message", "未处理的错误!");
                        return result;
                    //logger.error("未处理的错误,code=" + statusCode);
                }

            } finally {
                if (response != null) {
                    try {
                        ((CloseableHttpResponse) response).close();
                    } catch (IOException e) {
                        logger.error("关闭响应错误.", e);
                    }
                }
            }
            //return result;
        }

    }

    /**
     * 连接处理,释放连接池连接
     *
     * @author 王
     */
    public static class IdleConnectionMonitorThread extends Thread {

        private static volatile boolean shutdown = false;
        private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;

        public IdleConnectionMonitorThread(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager) {
            this.poolingHttpClientConnectionManager = poolingHttpClientConnectionManager;
        }

        @Override
        public void run() {
            try {
                while (!shutdown) {
                    synchronized (IdleConnectionMonitorThread.class) {
                        wait(RELEASE_CONNECTION_WAIT_TIME);
                        // Close expired connections
                        poolingHttpClientConnectionManager.closeExpiredConnections();
                        // that have been idle longer than 30 sec
                        poolingHttpClientConnectionManager.closeIdleConnections(2000, TimeUnit.MINUTES);
                    }
                }
            } catch (InterruptedException ex) {
                logger.error("释放连接池连接出错.");
            }
        }

        public void shutdown() {
            shutdown = true;
            synchronized (IdleConnectionMonitorThread.class) {
                notifyAll();
            }
        }

    }
}
